<?php 

namespace app\admin\event;

use app\common\model\Adv as AdvModel;
use app\common\utils\JsonService;

class AdvEvent{

    /*
    * 广告列表
    */
    public static function advList(){
        $params = array_filter(request()->param());
        $page = ! empty($params['page']) && $params['page'] >= 1 ? $params['page'] : 1;
        $where = [];
        if($id = isset($params['id'])){
                    $where[] = [['id','=',$params['id']]];
            }
            if($ap_id = isset($params['ap_id'])){
                    $where[] = [['ap_id','like',"%{$params['ap_id']}%"]];
            }
            if($adv_title = isset($params['adv_title'])){
                    $where[] = [['adv_title','like',"%{$params['adv_title']}%"]];
            }
            if($adv_url = isset($params['adv_url'])){
                    $where[] = [['adv_url','like',"%{$params['adv_url']}%"]];
            }
            if($slide_sort = isset($params['slide_sort'])){
                    $where[] = [['slide_sort','like',"%{$params['slide_sort']}%"]];
            }
             $condition = [
            'where' => $where,
            'order' => ['id' => 'desc'],
                'paginate' => [
                'page' => $page,
                'query' => $params,
            ],
        ];
        $adv = AdvModel::getPageAll($condition);
        return $adv;
    }


    /*
    * 新增广告
    */
    public static function createAdv($data){

        try {
            AdvModel::create($data);
        } catch (\Exception $ex) {
            return JsonService::error($ex->getMessage());
        }
        return JsonService::success();
    }

    /*
    * 更新广告
    */
    public static function updateAdv($data){

        try {
            AdvModel::updateOne($data, ['id' => $data['id']]);
        } catch (\Exception $ex) {
            return JsonService::error($ex->getMessage());
        }
        return JsonService::success();
    }


}

